package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {

   //todo ： 分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //添加逻辑删除后 初始查询 要有select * from xxx where is_delete=0 and xxx like
        //构造条件 wrapper
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<CheckItem>();
        queryWrapper.eq("is_delete", 0);
        Page<CheckItem> page = null;
        //有条件分页
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {//如果查询条件为空 就执行无条件查询
            // 无条件分页查询
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        } else {//如果有查询条件

            queryWrapper.like("name", queryPageBean.getQueryString());//name是数据库字段名 后面是查询的条件
            //上面相当于 sql语句 加上了where name =? 条件
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;


    }


    //todo：逻辑删除
    @Override
    public Boolean updateCheckItemById(int id) {
        //逻辑删除 再查询时加上条件 Is_delete作的修改
        CheckItem checkItem = new CheckItem();
        checkItem.setId(id);
        checkItem.setIs_delete(1);//查询的条件
        return updateById(checkItem);
    }
}
